<?php
namespace app\admin\model;

use think\Model;
use think\facade\Session;
use think\facade\Db;
use think\facade\Cache;

class Category extends Model
{

    //自定义内容
    //增加与更新后动作
    public static function onAfterwrite($data){
        //图片绑定处理
        if(isset($data['thumb'])){
            Files::bindInfo($data['thumb'],$data['id'],$data['id'],'category','thumb');
        }
        $path='';
        $pid = $data->pid;
        if($pid){
            $parent = self::find($pid);
            $path=$parent->path . $pid . ',';
        }else{
            $path=0 . ',';
        }
        db::name('category')->where('id',$data->id)->update(['path'=>$path]);
    }

    //删除数据后操作
    public static function onAfterDelete($data)
    {
        //删除所有子栏目
        $category = self::where('path', 'like', '%,' . $data->id . ',%')->select();
        foreach ($category as $key => $value) {
            $value->delete();
        }
    }

    //查询后动作
    public static function onAfterRead($res){
        if(Session::get('category')=='getpathinfo'){
            $ids=$res->path.$res->id;
            $grouptitle=Db::name('category')->where('id','in',$ids)->column('id,title');
            $res['pathinfo']=$grouptitle;
        }
        return $res;
    }

    //下拉菜单
    public static function getSelected($pid, $ids,$modid)
    {
        $map=[];
        if($modid){
            $map[]=['mod_id','=',$modid];
        }
        $data = self::where($map)->whereRaw("FIND_IN_SET($pid,path)")->select();
        if ($ids) {
            array_unshift($ids, $pid);
        } else {
            $ids = [$pid];
        }
        $ids = array_unique($ids);
        $selectNode = [];
        foreach ($ids as $id) {
            $item = [];
            foreach ($data as $k => $v) {
                if ($v['pid'] == $id) {
                    $item[] = $v;
                }
            }
            if ($item)
                $selectNode[] = $item;
        }
        return $selectNode;
    }
    public function setThumbAttr($value,$data){
        if(isset($value['id'])){
            return $value['id'];
        }
        return $value;
    }
    public function getThumbAttr($value,$data){
        return Files::where('id',$value)->find();
    }
    public function getPositionAttr($value,$data){
        $path=$data['path'].$data['id'];
        $pathArr=explode(',',$path);
        $category = Cache::get('category')?Cache::get('category'):[];
        $pos=[];
        foreach($category as $v){
            if(empty($v['pinyin'])){
                continue;
            }
            if(in_array($v['id'],$pathArr)){
                $pos[$v['id']]=$v;
            }
        }
        sort($pos);
        $route='/';
        foreach($pos as $k=>$v){
            $route.=$v['pinyin'].'/';
            $pos[$k]['route']=$route;
        }


        return $pos;
    }
    public function Article(){
        return $this->hasMany(Article::class);
    }

    public function mod()
    {
        return $this->belongsTo(Mod::class);
    }
}
